package com.rytx.web.controller.ysp;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rytx.common.core.domain.model.LoginUser;
import com.rytx.framework.web.service.TokenService;
import com.rytx.system.domain.ArtTaxTemplate;
import com.rytx.system.service.IArtTaxTemplateService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.rytx.common.annotation.Log;
import com.rytx.common.core.controller.BaseController;
import com.rytx.common.core.domain.AjaxResult;
import com.rytx.common.enums.BusinessType;
import com.rytx.system.domain.ArtTax;
import com.rytx.system.service.IArtTaxService;
import com.rytx.common.utils.poi.ExcelUtil;
import com.rytx.common.core.page.TableDataInfo;

/**
 * 发票管理Controller
 * 
 * @author rytx
 * @date 2024-09-02
 */
@RestController
@RequestMapping("/system/tax")
public class ArtTaxController extends BaseController
{
    @Autowired
    private IArtTaxService artTaxService;
    @Autowired
    private IArtTaxTemplateService artTaxTemplateService;
    @Autowired
    private TokenService tokenService;

    @PostMapping("/taxAdd")
    @ApiImplicitParams({
            @ApiImplicitParam(name="takName",value="发票抬头",required=true),
            @ApiImplicitParam(name="taxNumber",value="公司税号",required=true),
            @ApiImplicitParam(name="address",value="详细地址",required=true),
            @ApiImplicitParam(name="mobile",value="手机号",required=true),
            @ApiImplicitParam(name="bank",value="开户银行",required=true),
            @ApiImplicitParam(name="bankNumber",value="银行账号",required=true),
            @ApiImplicitParam(name="userEmail",value="邮箱",required=true),
            @ApiImplicitParam(name="userId",value="创建用户id",required=true),
            @ApiImplicitParam(name="userType",value="创建用户类型 0 个人 1公司",required=true),
            @ApiImplicitParam(name="orderNo",value="订单编号",required=true),
            @ApiImplicitParam(name="status",value="开发票状态",required=true),
            @ApiImplicitParam(name="amount",value="充值金额",required=true),
            @ApiImplicitParam(name="companyId",value="公司id",required=true),
            @ApiImplicitParam(name="takType",value="开票类型 0 个人 1 企业",required=true)
    })
    @ResponseBody
    public AjaxResult taxAdd(ArtTax artTax,HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        artTax.setUserId(userId);
        return toAjax(artTaxService.insertArtTax(artTax));
    }
    /**
     * 添加模板
     * */
    @PostMapping("/taxAddmoban")
    @ApiImplicitParams({
            @ApiImplicitParam(name="takName",value="发票抬头",required=true),
            @ApiImplicitParam(name="taxNumber",value="公司税号",required=true),
            @ApiImplicitParam(name="address",value="详细地址",required=true),
            @ApiImplicitParam(name="mobile",value="手机号",required=true),
            @ApiImplicitParam(name="bank",value="开户银行",required=true),
            @ApiImplicitParam(name="bankNumber",value="银行账号",required=true),
            @ApiImplicitParam(name="userEmail",value="邮箱",required=true),
            @ApiImplicitParam(name="userId",value="创建用户id",required=true),
            @ApiImplicitParam(name="userType",value="创建用户类型 0 个人 1公司",required=true),
            @ApiImplicitParam(name="status",value="开发票状态",required=true),
            @ApiImplicitParam(name="takType",value="开票类型 0 个人 1 企业",required=true)
    })
    @ResponseBody
    public AjaxResult taxAddmoban(ArtTaxTemplate artTaxTemplate,HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        artTaxTemplate.setUserId(userId);
        return   toAjax(artTaxTemplateService.insertArtTaxTemplate(artTaxTemplate));
    }
    /**
     * 查询所有模板用户模板 根据userid 查询
     * */
    @GetMapping("/getUserTaxTemplate")
    @ResponseBody
    public AjaxResult getUserTaxTemplate(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        ArtTaxTemplate artTaxTemplate = new ArtTaxTemplate();
        artTaxTemplate.setUserId(userId);
        return  AjaxResult.success(artTaxTemplateService.selectArtTaxTemplateList(artTaxTemplate));
    }
    /**
     * 查询用户模板 根据id 查询
     * */
    @GetMapping("/getUserTaxTemplateById")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="模板的id",required=true),
    })
    @ResponseBody
    public AjaxResult getUserTaxTemplateById(Long id)
    {
        return  AjaxResult.success(artTaxTemplateService.selectArtTaxTemplateById(id));
    }
    /**
     * 编辑模板
     * */
    @PostMapping("/editAddmoban")
    @ResponseBody
    public AjaxResult editAddmoban(ArtTaxTemplate artTaxTemplate,HttpServletRequest request)
    {
        return  toAjax(artTaxTemplateService.updateArtTaxTemplate(artTaxTemplate));
    }
    /**
     * 删除模板
     * */
    @GetMapping("/deleteUserTaxTemplate")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="模板的id",required=true),
    })
    @ResponseBody
    public AjaxResult deleteUserTaxTemplate(Long id)
    {
        return   toAjax(artTaxTemplateService.deleteArtTaxTemplateById(id));
    }
    /**
     * 根据用户id查询该用户的 申请发票的列表
     * */
    @GetMapping("/getTaxListByUserId")
    @ResponseBody
    public AjaxResult getTaxListByUserId(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        ArtTax artTax = new ArtTax();
        artTax.setUserId(userId);
        return  AjaxResult.success(artTaxService.selectArtTaxList(artTax));
    }
    /**
     * 根据takid 查询tak详情
     * */
    @GetMapping("/getTaxById")
    @ResponseBody
    public AjaxResult getTaxById(Long id)
    {
        return  AjaxResult.success( artTaxService.selectArtTaxById(id));
    }
    /**
     * 根据订单编号查询开发票记录
     * */
    @GetMapping("/getTaxByOrderno")
    @ApiImplicitParams({
            @ApiImplicitParam(name="orderNo",value="订单号",required=true),
    })
    @ResponseBody
    public AjaxResult getTaxByOrderno(String orderNo)
    {
        ArtTax artTax = artTaxService.getTaxByOrderno(orderNo);
        return  AjaxResult.success(artTax);
    }


    //从这里往下是后管的接口
    /**
     * 查询发票管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:tax:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArtTax artTax)
    {
        startPage();
        List<ArtTax> list = artTaxService.selectArtTaxList(artTax);
        return getDataTable(list);
    }
    /**
     * 完成开发票
     */
    @PostMapping("/complete")
    public AjaxResult complete(@RequestBody ArtTax artTax)
    {
        return toAjax(artTaxService.complete(artTax));
    }
    /**
     * 导出发票管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:tax:export')")
    @Log(title = "发票管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArtTax artTax)
    {
        List<ArtTax> list = artTaxService.selectArtTaxList(artTax);
        ExcelUtil<ArtTax> util = new ExcelUtil<ArtTax>(ArtTax.class);
        util.exportExcel(response, list, "发票管理数据");
    }

    /**
     * 获取发票管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tax:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(artTaxService.selectArtTaxById(id));
    }

    /**
     * 新增发票管理
     */
    @PreAuthorize("@ss.hasPermi('system:tax:add')")
    @Log(title = "发票管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArtTax artTax)
    {
        return toAjax(artTaxService.insertArtTax(artTax));
    }

    /**
     * 修改发票管理
     */
    @PreAuthorize("@ss.hasPermi('system:tax:edit')")
    @Log(title = "发票管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArtTax artTax)
    {
        return toAjax(artTaxService.updateArtTax(artTax));
    }

    /**
     * 删除发票管理
     */
    @PreAuthorize("@ss.hasPermi('system:tax:remove')")
    @Log(title = "发票管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(artTaxService.deleteArtTaxByIds(ids));
    }
}
